package cn.ssms.controller;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssms.model.Image;
import cn.ssms.model.ResultObject;
import cn.ssms.service.ImageUploadService;
import cn.ssms.util.CheckUtil;
import cn.ssms.util.ReqParams;
import cn.ssms.util.SHA1;
@Controller
public class ImageServeController {
	private CheckUtil checkUtil = new CheckUtil();
	@Autowired
	private ImageUploadService imageUploadService;
	@RequestMapping("/image")
	@ResponseBody
	//ResultObject<Image>
	public JSONPObject  imageMachine(HttpServletRequest req) {
	
		ResultObject<Image> result = new ResultObject<Image>();
		String bucket=ReqParams.bucket;//存储空间
		String url = req.getParameter("imgUrl");// 图片的URL	
		String tag = req.getParameter("tag");//标签
		String imageName = req.getParameter("imgTitle");// 标题
		String categoryCode = req.getParameter("categoryCode");//类型
		String author = req.getParameter("author");//作者
		String header=req.getHeader("Request URL");
		System.out.println("获取相应的头：：："+header);
		// 验证url是否有效
		try {
			if (null!=url && !"".equals(url) && checkUtil.IsHttpUrlConnection(url)) {
				Image img = new Image();
				img.setImgId(SHA1.getSHA1(url));
				img.setImgUrl(url);
				img.setSource(URI.create(url).getHost());// 原始来源站点
				img.setSourceUrl(url);
				if(author!=null){
					img.setAuthor(author);
				}
				img.setRealUrl(url);
				img.setCategoryCode(categoryCode);
				if(imageName!=null){
					img.setImgTitle(imageName);
				}
				if(tag!=null){
					img.setTag(tag);
				}
				img.setIsCopyRight("0");// 是否原创(1是0否) 默认0
				img.setIsIndex("0");// 是否建立索引   默认0
				img.setIsReal("0");// 是否下载（存放到bigwe图片服务  1下0未） 默认0
				img.setIsValid("1");// 是否有效(1有0无) 默认0
				img.setDataTime(new Date());// 图片数据录入时间
				boolean flag = imageUploadService.addImage(img);

				if(flag){
					// 七牛返回的URL路径
					String newImgUrl = imageUploadService.imageQiNiuUrl(url,imageName,bucket);
					System.out.println("newImgUrl"+newImgUrl);
					//判断七牛返回的图片连接是否为空
					if (null!=newImgUrl && !newImgUrl.isEmpty()) {
						img.setIsReal("1");// 设置可下载
						img.setRealUrl(newImgUrl);
						// 将图片原始路径替换为七牛的Url路径
						imageUploadService.updateImage(img);
						result.setCode("000");
						result.setData(img);
						result.setMassage("成功");
					}
				}else{
					Image toUrl=imageUploadService.selectOneImageToId(SHA1.getSHA1(url));//id is_real
					if(null!=toUrl){
						result.setCode("000");
						result.setData(toUrl);
						result.setMassage("成功");
					}else{
						String newImgUrl = imageUploadService.imageQiNiuUrl(url,imageName,bucket);
						img.setRealUrl(newImgUrl);
						imageUploadService.updateImage(img);
						result.setCode("000");
						result.setData(img);
						result.setMassage("失败");
					}
				}
			}else{
				result.setCode("000");
				result.setData(null);
				result.setMassage("URL不为Image的连接或者URL不合法");
			}
		}
		catch (IOException e) {
			result.setCode("000");
			result.setData(null);
			result.setMassage("失败");
		}
		return new JSONPObject("成功", result.getData());
	}

	public static void main(String[] args) throws UnknownHostException {

		System.out.println(URI.create("http://img01.taopic.com/150920/240455-1509200H31810.jpg").getHost());
		String hostName=URI.create("http://img01.taopic.com/150920/240455-1509200H31810.jpg").getHost();
		InetAddress addr=InetAddress.getByName(hostName);
		System.out.println(addr.getHostAddress());
	}
}
